
//
//  GoodsCollectionViewCell.m
//  News
//
//  Created by weige on 16/6/1.
//  Copyright © 2016年 weige. All rights reserved.
//

#import "GoodsCollectionViewCell.h"
#import "Goods.h"
#import "WYAddToCartAnimation.h"
/*!
 *  @author weige, 16-06-01 10:06:16
 *
 *  @brief 商品
 */
@interface GoodsCollectionViewCell ()
{
    CGSize marksize;
    CGSize shijisize;
}
@end
@implementation GoodsCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        
        [self goodsViewUI];
    }
    return self;
}

/*!
 *  @author weige, 16-06-02 09:06:04
 *
 *  @brief 创建UI
 */
-(void)goodsViewUI
{
    self.backgroundColor=[UIColor whiteColor];
    self.goodsImage=[[UIImageView alloc]init];
    self.goodsImage.userInteractionEnabled=YES;
    self.goodsTitle=[[UILabel alloc]init];
    self.goodsTitle.textAlignment=NSTextAlignmentCenter;
    self.goodsTitle.font=[UIFont systemFontOfSize:GoodsFontSize];
    self.fineImageView=[[UIImageView  alloc]init];
    self.fineImageView.userInteractionEnabled=YES;
    self.givedesc=[[UIImageView alloc]init];
    self.givedesc.hidden=YES;
    self.fineImageView.hidden=YES;
    self.laspecifics=[[UILabel alloc]init];
    self.laspecifics.textColor=TITLECOLOR;
    self.laspecifics.font=[UIFont systemFontOfSize:10];
    //self.laspecifics.text=@"$12.00";
   // self.price=[[PriceView alloc]init];
    
    //[self addSubview:self.price];
    
    [self addSubview:self.goodsImage];
    [self addSubview:self.goodsTitle];
    [self addSubview:self.fineImageView];
    [self addSubview:self.givedesc];
    [self addSubview:self.laspecifics];
    self.currentPrice=[[UILabel alloc]init];
    self.currentPrice.font=[UIFont systemFontOfSize:10];
    self.currentPrice.textColor=[UIColor redColor];
    //self.currentPrice.backgroundColor=[UIColor grayColor];
    
    [self addSubview:self.currentPrice];
    self.markPrice=[[UILabel alloc]init];
    self.markPrice.font=[UIFont systemFontOfSize:10];
    self.markPrice.textColor=TITLECOLOR;
    [self addSubview:self.markPrice];
    self.line=[[UIView alloc]init];
    self.line.backgroundColor=[UIColor lightGrayColor];
    [self addSubview: self.line];
    self.btn=[[BuyView alloc]init];
   
    [self addSubview:self.btn];

    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.goodsImage.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.width);
    self.goodsTitle.frame=CGRectMake(5, self.frame.size.width, self.frame.size.width-15, 20);
    self.fineImageView.frame=CGRectMake(10, CGRectGetMaxY(self.goodsTitle.frame)+4, 30, 15);
    self.givedesc.frame=CGRectMake(CGRectGetMaxX(self.fineImageView.frame)+5, CGRectGetMaxY(self.goodsTitle.frame)+4, 30, 15);
    self.laspecifics.frame=CGRectMake(10, CGRectGetMaxY(self.givedesc.frame),self.frame.size.width-20, 20);
    self.currentPrice.frame=CGRectMake(10, CGRectGetMaxY(self.laspecifics.frame)-4, shijisize.width, 20);
    self.markPrice.frame=CGRectMake(CGRectGetMaxX(self.currentPrice.frame)+3, CGRectGetMaxY(self.laspecifics.frame)-4, marksize.width, 20);
     self.line.frame=CGRectMake(CGRectGetMaxX(self.currentPrice.frame)+4, CGRectGetMaxY(self.markPrice.frame)-4-7+0.5, marksize.width, 1);
    self.btn.frame=CGRectMake(self.frame.size.width-85, self.frame.size.height-40, 80, 25);
   }
-(void)setGood:(Goods *)good
{
    
    //NSLog(@"我想看看%@",good.ID);
    [self.goodsImage sd_setImageWithURL:[NSURL URLWithString:good.img] placeholderImage:[UIImage imageNamed:@"v2_placeholder_square"]];
    self.goodsTitle.text=good.name;
    if ([good.is_xf isEqual:@1] ) {
        self.fineImageView.hidden=NO;
        self.fineImageView.image=[UIImage imageNamed:@"jingxuan.png"];
    }
    else
    {
        self.fineImageView.hidden=YES;
    }
    /*!
     *  @author weige, 16-06-02 11:06:36
     *
     *  @brief 判断是否存在
     */
    if (![good.pm_desc isEqualToString:@"买一赠一"]) {
        self.givedesc.hidden=YES;
    }
    else
    {
        self.givedesc.hidden=NO;
        self.givedesc.image=[UIImage imageNamed:@"buyOne.png"];
    }
    if (good.specifics !=nil) {
        self.laspecifics.text=good.specifics;
    }
    self.currentPrice.text=[NSString stringWithFormat:@"￥:%@",good.partner_price];
    shijisize=[[NSString stringWithFormat:@"￥:%@",good.partner_price] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]}];
    if (good.market_price ==good.partner_price) {
        self.markPrice.hidden=YES;
        self.line.hidden=YES;
    }
    else
    {
        self.markPrice.hidden=NO;
        self.line.hidden=NO;
        self.markPrice.text=[NSString stringWithFormat:@"￥:%@",good.market_price];
        NSString *str= [NSString stringWithFormat:@"￥:%@",good.market_price ];
        marksize=  [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]}];
        [self.markPrice sizeToFit];
    }
    self.btn.goods=good;
    self.btn.imageGood=self.goodsImage;
   
    //self.btn
   // [self addButtonClick:self.goodsImage];
    
}


@end

